<?php

namespace App\Http\Controllers\Api\Merchant;

use App\AboutUs;
use App\AuthorizerInfo;
use App\AvailableUrl;
use App\Classify;
use App\Enterprise;
use App\EnterpriseAdmin;
use App\Http\Controllers\Api\Admin\PlatformController;
use App\Http\Controllers\Api\WeChatThirdController;
use App\Http\Middleware\Role;
use App\Http\Services\CommonManager;
use App\Http\Services\FileManager;
use App\Http\Services\MerchantManager;
use App\Merchant;
use App\MerchantDeliveryMethod;
use App\MerchantMedicine;
use App\MerchantStaff;
use App\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use vendor\project\StatusTest;

class MerchantController extends Controller
{
    /**
     * 门店列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $merchantId = 0;
        if($MerchantUser->role == 3){
            $merchantStaff = MerchantStaff::whereUserId($MerchantUser->id)->first();
            if($merchantStaff){
                $merchantId = $merchantStaff->merchant_id;
            }
        }


        $page    = !is_null($request->get('page')) ? $request->get('page') : 1;
        $limit   = !is_null($request->get('limit')) ? $request->get('limit') : 16;

        $pageInfo = [
            'page' => $page,
            'limit' => $limit,
        ];

        $merchantList = Merchant::whereEnterpriseId($enterpriseInfo->id);

        if($merchantId != 0){
            $merchantList->whereId($merchantId);
        }

        $pageInfo['total'] = $merchantList->count();

        $merchantList = $merchantList->orderByDesc('created_at')->offset($limit * ($page - 1))->limit($limit)->get();
        if(count($merchantList) > 0){
            foreach ($merchantList as $merchant) {
                $merchant->logo_img_Url = FileManager::getImgUrl($merchant->logo_img_id);
                if($merchant->status == 1){
                    $merchant->status_name = '正常营业';
                }elseif($merchant->status == 2){
                    $merchant->status_name = '店铺已关闭';
                }else{
                    $merchant->status_name = '不显示';
                }

                $merchantDeliveryMethodArr = [];
                $merchantDeliveryMethod = MerchantDeliveryMethod::whereMerchantId($merchant->id)->get();
                if(count($merchantDeliveryMethod) > 0){
                    foreach ($merchantDeliveryMethod as $val) {
                        if($val->delivery_method == 'self_pick'){
                            $valArr = [$val->delivery_method];
                        }elseif($val->delivery_method == 'express_delivery'){
                            $valArr = [$val->delivery_method,$val->delivery_fee];
                        }elseif($val->delivery_method == 'instant_delivery'){
                            $valArr = [$val->delivery_method,$val->delivery_range,$val->start_delivery_fee,$val->delivery_fee];
                        }else{
                            $valArr = [];
                        }

                        array_push($merchantDeliveryMethodArr,$valArr);
                    }
                }

                $merchant->merchant_delivery_method = $merchantDeliveryMethodArr;
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchantList,'page_info' => $pageInfo]);
    }

    /**
     * 门店详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantDetail(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->input('id') ?? 0;

        $merchant = Merchant::whereId($id)->first();
        if($merchant){
            $merchant->logo_img_Url = FileManager::getImgUrl($merchant->logo_img_id);

            if($merchant->status == 1){
                $merchant->status_name = '营业中';
            }elseif($merchant->status == 2){
                $merchant->status_name = '店铺已关闭';
            }else{
                $merchant->status_name = '休息';
            }

            $merchantDeliveryMethodArr = [];
            $merchantDeliveryMethod = MerchantDeliveryMethod::whereMerchantId($merchant->id)->get();
            if(count($merchantDeliveryMethod) > 0){
                foreach ($merchantDeliveryMethod as $val) {
                    if($val->delivery_method == 'self_pick'){
                        $valArr = [$val->delivery_method];
                    }elseif($val->delivery_method == 'express_delivery'){
                        $valArr = [$val->delivery_method,$val->delivery_fee];
                    }elseif($val->delivery_method == 'instant_delivery'){
                        $valArr = [$val->delivery_method,$val->delivery_range,$val->start_delivery_fee,$val->delivery_fee];
                    }else{
                        $valArr = [];
                    }

                    array_push($merchantDeliveryMethodArr,$valArr);
                }
            }

            $merchant->merchant_delivery_method = $merchantDeliveryMethodArr;
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchant]);
    }

    /**
     * 编辑门店
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setMerchant(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $name                  = $request->get('name') ?? '';
        $logoImgId             = $request->get('logo_img_id') ?? 0;
        $province              = $request->get('province') ?? '';
        $city                  = $request->get('city') ?? '';
        $area                  = $request->get('area') ?? '';
        $address               = $request->get('address') ?? '';
        $contact               = $request->get('contact') ?? '';
        $businessTime          = $request->get('business_time') ?? '';
        $businessStartTime     = $request->get('business_start_time') ?? '';
        $businessEndTime       = $request->get('business_end_time') ?? '';
        $lng                   = $request->get('lng') ?? '';
        $lat                   = $request->get('lat') ?? '';
        $status                = $request->get('status') ?? 0;
        $id                    = $request->get('id') ?? 0;

        $merchant = Merchant::whereEnterpriseId($enterpriseInfo->id)->whereId($id)->first();
        if(!$merchant)
            $merchant = new Merchant();

        $merchant->enterprise_id           = $enterpriseInfo->id;
        $merchant->name                    = $name;
        $merchant->logo_img_id             = $logoImgId;
        $merchant->province                = $province;
        $merchant->city                    = $city;
        $merchant->area                    = $area;
        $merchant->address                 = $address;
        $merchant->contact                 = $contact;
        $merchant->business_time           = $businessTime;
        if(!empty($businessStartTime))
            $merchant->business_start_time     = $businessStartTime;

        if(!empty($businessEndTime))
            $merchant->business_end_time       = $businessEndTime;
        $merchant->lng                     = $lng;
        $merchant->lat                     = $lat;
        $merchant->status                  = $status;
        $merchant->review                  = 1;
        $merchant->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $merchant]);
    }

    /**
     * 删除门店
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delMerchant(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $merchant = Merchant::whereId($id)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '该门店不存在']);

        $merchant->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $merchant]);
    }

    /**
     * 改变营业状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeMerchantStatus(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id     = $request->get('id') ?? 0;
        $status = $request->get('status') ?? 0;

        $merchant = Merchant::whereId($id)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '该门店不存在']);

        if(!in_array($status,[0,1,2]))
            return response()->json(['level' => 'error','message' => '操作失败']);

        $merchant->status = $status;
        $merchant->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $merchant]);
    }

    /**
     * 设置配送方式
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setMerchantDeliveryMethod(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;
        $deliveryMethod = $request->get('delivery_method') ?? [];

        if(!is_array($deliveryMethod) || (is_array($deliveryMethod) && count($deliveryMethod) <= 0))
            return response()->json(['level' => 'error','message' => '请选择配送方式']);

        $merchant = Merchant::whereId($id)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '门店不存在']);

        $merchantDeliveryMethod = MerchantDeliveryMethod::whereMerchantId($merchant->id)->get();
        if(count($merchantDeliveryMethod) > 0){
            foreach ($merchantDeliveryMethod as $item) {
                $item->delete();
            }
        }
        $method=[];

        foreach ($deliveryMethod as $val) {
            $merchantDeliveryMethod = new MerchantDeliveryMethod();
            $merchantDeliveryMethod->merchant_id        = $merchant->id;
            $merchantDeliveryMethod->delivery_method    = $val[0] ? $val[0] : '';

            if($val[0] == 'express_delivery'){
                $merchantDeliveryMethod->delivery_fee = $val[1] ? $val[1] : 0;
            }elseif($val[0] == 'instant_delivery'){
                $merchantDeliveryMethod->delivery_range = $val[1] ? $val[1] : 0;
                $merchantDeliveryMethod->start_delivery_fee = $val[2] ? $val[2] : 0;
                $merchantDeliveryMethod->delivery_fee = $val[3] ? $val[3] : 0;
            }

            $merchantDeliveryMethod->save();
            array_push($method,$merchantDeliveryMethod->delivery_method);
        }
        $merchant->support_delivery_method=json_encode($method);
        $merchant->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $merchant]);
    }

    /**
     * 商品分类列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGoodsClassifyList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $page       = !is_null($request->get('page')) ? $request->get('page') : 1;
        $limit      = !is_null($request->get('limit')) ? $request->get('limit') : 16;
        $merchantId = $request->get('merchant_id') ?? 0;
        $pid        = $request->get('pid') ?? 0;

        $pageInfo = [
            'page' => $page,
            'limit' => $limit,
        ];

        $goodsClassifyList = Classify::wherePid($pid)->whereMerchantId($merchantId)->whereType('selling');

        $pageInfo['total'] = $goodsClassifyList->count();

        $goodsClassifyList = $goodsClassifyList->orderBy('sort')->orderByDesc('created_at')->offset($limit * ($page - 1))->limit($limit)->get();
        if(count($goodsClassifyList) > 0){
            foreach ($goodsClassifyList as $goodsClassify) {
                $goodsClassify->img_url = FileManager::getImgUrl($goodsClassify->img_id);

                if($goodsClassify->status == 1){
                    $goodsClassify->status_name = '开启';
                }else{
                    $goodsClassify->status_name = '关闭';
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $goodsClassifyList,'page_info' => $pageInfo]);
    }

    /**
     * 商品分类详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGoodsClassifyDetail(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $goodsClassify = Classify::whereId($id)->first();
        if($goodsClassify){
            $goodsClassify->img_url = FileManager::getImgUrl($goodsClassify->img_id);
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $goodsClassify]);
    }

    /**
     * 编辑商品分类
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setGoodsClassify(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $name       = $request->get('name') ?? '';
        $desc       = $request->get('desc') ?? '';
        $pid        = $request->get('pid') ?? 0;
        $merchantId = $request->get('merchant_id') ?? 0;
        $imgId      = $request->get('img_id') ?? 0;
        $sort       = $request->get('sort') ?? 0;
        $status     = $request->get('status') ?? 0;
        $linkUrl    = $request->get('link_url') ?? '';
        $id         = $request->get('id') ?? 0;

        $goodsClassify = Classify::whereId($id)->first();
        if(!$goodsClassify)
            $goodsClassify = new Classify();

        $goodsClassify->enterprise_id = $enterpriseInfo->id;
        $goodsClassify->pid           = $pid;
        $goodsClassify->merchant_id   = $merchantId;
        $goodsClassify->name          = $name;
        $goodsClassify->desc          = $desc;
        $goodsClassify->type          = 'selling';
        $goodsClassify->link_url      = $linkUrl;
        $goodsClassify->img_id        = $imgId;
        $goodsClassify->sort          = $sort;
        $goodsClassify->status        = $status;
        $goodsClassify->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $goodsClassify]);
    }

    /**
     * 删除商品分类
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delGoodsClassify(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $goodsClassify = Classify::whereId($id)->first();
        if(!$goodsClassify)
            return response()->json(['level' => 'error','message' => '分类不存在']);

        $goodsClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $goodsClassify]);
    }

    /**
     * 门店员工列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantStaffList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $page       = !is_null($request->get('page')) ? $request->get('page') : 1;
        $limit      = !is_null($request->get('limit')) ? $request->get('limit') : 16;
        $keyword    = $request->get('keyword') ?? '';
        $merchantId = $request->get('merchant_id') ?? 0;

        $pageInfo = [
            'page' => $page,
            'limit' => $limit,
            'keyword' => $keyword
        ];

        $merchantStaffList = MerchantStaff::whereMerchantId($merchantId);

        if($keyword != ''){
            $userIds = User::where('admin_name','like','%'.trim($keyword).'%')->orWhere('phone','like','%'.trim($keyword).'%')->pluck('id');

            $merchantStaffList->whereIn('user_id',$userIds);
        }

        $pageInfo['total'] = $merchantStaffList->count();

        $merchantStaffList = $merchantStaffList->orderByDesc('created_at')->offset($limit * ($page - 1))->limit($limit)->get();
        if(count($merchantStaffList) > 0){
            foreach ($merchantStaffList as $merchantStaff) {
                $merchantStaff->realname = '';
                $merchantStaff->role_name = '';
                $merchantStaff->username = '';

                $staffUser = User::whereId($merchantStaff->user_id)->first();
                if($staffUser){
                    $merchantStaff->realname = $staffUser->admin_name;
                    $merchantStaff->username = $staffUser->username;
                    if(empty($merchantStaff->phone)){
                        $merchantStaff->phone = $staffUser->phone;
                    }
                }

                $roleUser = DB::table('role_user')->where('user_id',$merchantStaff->user_id)->first();
                if($roleUser){
                    $role = \App\Role::whereId($roleUser->role_id)->first();
                    if($role){
                        $merchantStaff->role_name = $role->display_name;
                    }
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchantStaffList,'page_info' => $pageInfo]);
    }

    /**
     * 门店员工详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantStaffDetail(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $id = $request->get('id') ?? 0;

        $merchantStaff = MerchantStaff::whereId($id)->first();
        if($merchantStaff){
            $merchantStaff->realname = '';
            $merchantStaff->username = '';

            $staffUser = User::whereId($merchantStaff->user_id)->first();
            if($staffUser){
                $merchantStaff->realname = $staffUser->admin_name;
                $merchantStaff->username = $staffUser->username;
                if(empty($merchantStaff->phone)){
                    $merchantStaff->phone = $staffUser->phone;
                }
            }

            $roleUsers = DB::table('role_user')->where('user_id',$merchantStaff->user_id)->get();
            $roleIds = array();
            if(count($roleUsers) > 0){
                foreach($roleUsers as $roleUser){
                    /** @var \App\Role $role */
                    $role = \App\Role::whereId($roleUser->role_id)->first();
                    if($role){
                        $roleObj = $role->id;
                        array_push($roleIds,$roleObj);
                    }

                }
            }

            $merchantStaff->role_id = $roleIds;
        }

        /** @var Role $permission */
        $roles = \App\Role::whereEnterpriseId($enterpriseInfo->id)->whereType('enterprise')->where('name','<>','merchant_admin')->get();
        $roleArr = array();
        if(count($roles) > 0){
            foreach($roles as $role){
                $roleInfo = (object)['id' => $role->id,'role' => $role->display_name];
                array_push($roleArr,$roleInfo);
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchantStaff,'role_list' => $roleArr]);
    }

    /**
     * 编辑门店员工
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setMerchantStaff(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $username   = $request->get('username') ?? '';
        $password   = $request->get('password') ?? '';
        $realname   = $request->get('realname') ?? '';
        $phone      = $request->get('phone') ?? '';
        $roleIds    = $request->get('role_id');
        $status     = $request->get('status') ?? 1;
        $id         = $request->get('id') ?? 0;
        $merchantId = $request->get('merchant_id') ?? 0;

        if(!is_array($roleIds) || (is_array($roleIds) && count($roleIds) <= 0))
            return response()->json(['level' => 'error','message' => '请选择角色']);

        /** @var User $hasUser */
        $hasUser = User::whereUsername(trim($username))->first();

        /** @var User $thatUser */
        $thatUser = User::whereId($id)->first();

        if($thatUser && ($hasUser && $thatUser->id != $hasUser->id))
            return response()->json(['level' => 'error','message' => '当前员工账号已存在']);

        if($phone != '' && !is_null($phone)){
            $phoneVerification = CommonManager::phoneVerification(trim($phone));
            if(!$phoneVerification)
                return response()->json(['level' => 'error','message' => '手机号格式不正确']);

            $isPhone = User::wherePhone(trim($phone))->whereIn('role',[2,3])->first();

            if($thatUser && ($isPhone && $thatUser->id != $isPhone->id))
                return response()->json(['level' => 'error','message' => '当前手机号已被使用']);
        }

        if(!$thatUser){
            if($hasUser)
                return response()->json(['level'=>'error','message' => '当前管理员账号已存在']);

            if($phone != '' && !is_null($phone)) {
                if ($isPhone)
                    return response()->json(['level' => 'error', 'message' => '当前手机号已被使用']);
            }
//            if(is_null($repass) && $repass == '')
//                return response()->json(['level' => 'error','message' => '请再次输入密码']);
//
//            if($password != $repass)
//                return response()->json(['level'=>'error','message' => '两次密码不一致！']);

            $thatUser = new User();
            $thatUser->user_key = md5($password . rand(1000,9999).time());
        }

        $thatUser->username    = $username;
        $thatUser->phone       = $phone;
        $thatUser->admin_name  = $realname;

        if(!empty($password))
            $thatUser->password    = bcrypt($password);

        $thatUser->role        = 3;
        $thatUser->reg_ip      = $request->ip();
        $thatUser->reg_time    = date("Y-m-d H:i:s",time());
        $thatUser->status      = $status;
        $thatUser->save();

        $merchantStaff = MerchantStaff::whereUserId($thatUser->id)->first();
        if(!$merchantStaff){
            $newMerchantStaff = new MerchantStaff();
            $newMerchantStaff->user_id          = $thatUser->id;
            $newMerchantStaff->phone            = $phone;
            $newMerchantStaff->merchant_id      = $merchantId;
            $newMerchantStaff->save();
        }

        DB::table('role_user')->where('user_id',$thatUser->id)->delete();

        foreach($roleIds as $roleId){
            $role = \App\Role::whereId($roleId)->first();
            if($role){
                DB::table('role_user')->insert(['user_id' => $thatUser->id,'role_id' => $roleId]);
            }
        }

        return response()->json(['level'=>'success','message'=>'添加成功','data'=>$thatUser]);
    }

    /**
     * 删除门店员工
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delMerchantStaff(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $merchantStaff = MerchantStaff::whereId($id)->first();
        if(!$merchantStaff)
            return response()->json(['level' => 'error','message' => '该员工不存在']);

        $user = User::whereId($merchantStaff->user_id)->first();
        if($user){
            $user->delete();
        }

        $merchantStaff->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $merchantStaff]);
    }

    /**
     * 设置关于我们
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setAboutUs(Request $request){
        $detail = !is_null($request->get('detail')) ? $request->get('detail') : '';

        $newAboutUs = AboutUs::whereType('about_us')->first();
        if(!$newAboutUs)
            $newAboutUs = new AboutUs();

        $newAboutUs->type         = 'about_us';
        $newAboutUs->detail       = $detail;
        $newAboutUs->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newAboutUs]);
    }
    /**
     * 获取关于我们
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAboutUs(Request $request){

        $newAboutUs = AboutUs::whereType('about_us')->first();
        if(!$newAboutUs){
            $newAboutUs = new AboutUs();
            $newAboutUs->type         = 'about_us';
            $newAboutUs->detail       = '';
            $newAboutUs->save();
        }
        return response()->json(['level' => 'success','message' => '获取成功','data' => $newAboutUs]);
    }

    public function getMerchantQrCode(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $merchant_id = $request->input('merchant_id');
        $scene = 'mid='.$merchant_id;
        $option = [
            'page'=>'pages/index/index'
        ];
       $miniprogram = AuthorizerInfo::getMiniProgramInstance(AvailableUrl::getAppid($enterpriseInfo->id));
       if (!$miniprogram)
           return response()->json(['level' => 'error','message' => '无小程序信息']);
        $code = $miniprogram->app_code->getUnlimit($scene,$option);
        return response()->make($code,$code->getStatusCode(),$code->getHeaders());
    }
}
